<?php

namespace App\Admin\Controllers;

use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;
use App\Models\App;

class AppBaseController extends Controller
{
    /**
     * @var array
     */
	protected $app;
    /**
     * @var string
     */
	protected $title;
    /**
     * Title for current resource.
     *
     * @var string
     */
	public function __construct(){
		$this->app = get_app($this->name);
		$this->title = $this->app['title'];
	}


    /**
     * Set breadcrumb for following 4 action pages.
     *
     * @var array
     */
    public function breadcrumb(){
		  return [
			  ['text' => '项目应用', 'url' => 'item'],
			  ['text' => $this->title,'url'=>'apps/'.$this->name]
		  ];
	  }

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
		return $content
            ->title($this->title)
			->breadcrumb($this->breadcrumb()[0],$this->breadcrumb()[1])
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show(Content $content,$id)
    {
		$this->id = $id;
		return $content
        ->title($this->title)
		->breadcrumb($this->breadcrumb()[0],$this->breadcrumb()[1],['text'=>trans('admin.show')])
        ->description(trans('admin.show'))
        ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit(Content $content,$id)
    {

		    $this->id = $id;
		    return $content
            ->title($this->title)
			->breadcrumb($this->breadcrumb()[0],$this->breadcrumb()[1],['text'=>trans('admin.edit')])
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {

		    return $content
            ->title($this->title)
			->breadcrumb($this->breadcrumb()[0],$this->breadcrumb()[1],['text'=>trans('admin.create')])
            ->description(trans('admin.create'))
            ->body($this->form());
    }



    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        if(is_array($id) || count(str2arr($id))>1){
			$ids = str2arr($id);
			foreach($ids as $id){
				$this->form()->update($id);
			}
			return ["status" => true,"message" => "更新成功 !"];
		}else{
			return $this->form()->update($id);
		}
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return mixed
     */
    public function store()
    {
        return $this->form()->store();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

}
